package cn.tedu.antconlonysever.contract.schedule;


import cn.tedu.antconlonysever.contract.dao.persist.mapper.ContractMapper;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.LossMapper;
import cn.tedu.antconlonysever.contract.pojo.entity.Loss;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractListVo;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractToLoss;
import cn.tedu.antconlonysever.contract.pojo.vo.LossStandardVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Component
@Slf4j
/**
 * 合同定时任务类
 */
public class ContractSchedule {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private LossMapper lossMapper;

    /**
     * 每天凌晨 1点执行定时任务
     * @throws ParseException
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void contractSchedule() throws ParseException {
        log.info("************定时任务发生了！*****************");
        //1.查询该客户最后一份合同的创建时间
        List<ContractListVo> contractListVos = contractMapper.scheduleQueryList();
        for (ContractListVo contractListVo:contractListVos){
            //2.判断现在时间是否超过创建时间3天
            Date contractDate = contractListVo.getContractDate();
            SimpleDateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = sourceFormat.parse(contractDate + "");
            String format = targetFormat.format(date1);
            long parse = targetFormat.parse(format).getTime();
            //合同生效时间戳
//            Date contractDate1 = contractListVo.getContractDate();
//            long contractDate= simpleDateFormat.parse("Fri May 26 00:00:00 CST 2023").getTime();
            //合同生效3天后时间戳
            long afterContractDate =parse+(60L*1000*60*24*3);
            //获取当前时间戳
            long time = System.currentTimeMillis();
            //判断当前时间戳是否大于合同生效3天后时间
            //如果大于 就要将该客户信息加入流失表
            if (time>=afterContractDate){
                //根据客户名称查询客户信息
                ContractToLoss contractToLossByName = contractMapper.getContractToLossByName(contractListVo.getCusName());
                //判断流失表中是否存在该客户
                LossStandardVO lossStandardVOByon = lossMapper.getLossStandardVOByon(contractToLossByName.getNo());
                if (lossStandardVOByon!=null){
                    //如果占存表中已存在该用户，则跳过次循环
                    continue;
                }
                //不存在则将该用户信息加入 流失表
                Loss loss=new Loss();
                loss.setCusNo(contractToLossByName.getNo());
                loss.setCusName(contractToLossByName.getName());
                loss.setCusManager(contractToLossByName.getCusManager());
                loss.setLastOrderDate(contractListVo.getContractDate());
//                SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
//                String format1 = simpleDateFormat1.format(System.currentTimeMillis() + "");
//                Date parse1 = simpleDateFormat1.parse(format1);
                Date date = new Date(System.currentTimeMillis());
                loss.setConfirmLossDate(date);
                loss.setState(1);
                loss.setLossReason("3天");
                loss.setIsValid(1);
                loss.setGmtCreate(LocalDateTime.now());
                loss.setGmtModified(LocalDateTime.now());
                lossMapper.insert(loss);
            }
        }
    }

    //1.查询该客户最后一份合同的创建时间
    //2.判断现在时间是否超过创建时间3天
    //3.如果超过3天，则根据客户编号，名称查出该客户信息
    //4.将该客户信息写入，流失流失表
}
